var data = require("./data.js");
var show = require("./show.js");


const wallRate = 0.5;
const max_Wall_Del = 15;
var define = data.define;


function initMap(map, mapW, mapH, width, mapMouse) {
  var block_num = (mapH * mapW * wallRate);
  var i = 0;
  for (i = 0; i < block_num; i++) {
    var x = (Math.random() * mapH).toFixed() % mapH;
    var y = (Math.random() * mapW).toFixed() % mapW;
    map[x][y] = define.Wall;
  }
  adjustMap(map, mapW, mapH, width, mapMouse);
}

function adjustMap(map, mapW, mapH, width, mapMouse) {
  var i = 0;
  var j = 0;
  for (i = 1; i < mapH - 1; i++) {
    for (j = 1; j < mapW - 1; j++) {
      if (map[i][j] == 0) {
        travelMap(i, j, map, mapW, mapH, width, mapMouse);
      }
    }
  }
  for (i = 1; i < mapH - 1; i++) {
    for (j = 1; j < mapW - 1; j++) {
      if (map[i][j] != 0 && map[i][j] != define.Wall) {
        map[i][j] = 0;
      }
    }
  }
  drawWall(map, mapW, mapH, width, mapMouse);
}

function travelMap(x, y, map, mapW, mapH, width, mapMouse) {
  var temp = [];
  var wallPoint = [];
  var p = {};
  p.x = x;
  p.y = y;
  map[x][y] = define.GAS;

  var now_del = 0;

  temp.push(p);
  while (temp.length > 0) {

    var t = temp.pop();
    for (var i = 0; i < 4; i++) {
      var t1 = {};
      t1.x = t.x + define.Dir[i][0];
      t1.y = t.y + define.Dir[i][1];
      if (t1.x >= 1 && t1.x < mapH - 1 && t1.y >= 1 && t1.y < mapW - 1) {
        if (map[t1.x][t1.y] == 0) {
          map[t1.x][t1.y] = define.CL;
          temp.push(t1);
        }
        else if (map[t1.x][t1.y] == define.Wall)
          wallPoint.push(t1);
      }
    }
    p = t;

    if (temp.length == 0 && wallPoint.length > 0) {
      var t2 = wallPoint.pop();
      map[t2.x][t2.y] = define.TRAP;
      temp.push(t2);
      now_del++;
    }
    if (now_del > max_Wall_Del)
      break;
  }
}

function drawWall(map, mapW, mapH, width, mapMouse) {
  for (var i = 0; i < mapH; i++) {
    for (var j = 0; j < mapW; j++) {
      if (i == 0 || i == mapH - 1 || j == 0 || j == mapW - 1)
        map[i][j] = define.Wall;
      else if (map[i][j] != 0) {
        // map[i][j] = judgeWallBlock(i, j);
        map[i][j] = define.Wall;
      }
    }
  }
}

function judgeWallBlock(i, j, map, mapW, mapH, width, mapMouse) {
  var flag = [false, false, false, false];
  for (var c = 0; c < 4; c++) {
    if (map[define.Dir[c][0] + i][define.Dir[c][1] + j] != 0)
      flag[define.c] = true;
  }

  if (flag[define.Up] && flag[define.Down] && flag[define.Left] && flag[define.Right])
    return define.CROSS;

  ////////////////////////////////////////////////
  if (flag[define.Up] && flag[define.Down] && flag[define.Left] && !flag[define.Right])
    return define.LE;

  if (flag[define.Up] && flag[define.Down] && !flag[define.Left] && flag[define.Right])
    return define.RI;

  if (flag[define.Up] && !flag[define.Down] && flag[define.Left] && flag[define.Right])
    return define.UP;

  if (!flag[define.Up] && flag[define.Down] && flag[define.Left] && flag[define.Right])
    return define.DO;

  /////////////////////////////////////////////////
  if (flag[define.Up] && flag[define.Down] && !flag[define.Left] && !flag[define.Right])
    return define.COL;

  if (flag[define.Up] && !flag[define.Down] && flag[define.Left] && !flag[define.Right])
    return define.UL;

  if (!flag[define.Up] && flag[define.Down] && flag[define.Left] && !flag[define.Right])
    return define.DL;

  if (flag[define.Up] && !flag[define.Down] && !flag[define.Left] && flag[define.Right])
    return define.UR;

  if (!flag[define.Up] && flag[define.Down] && !flag[define.Left] && flag[define.Right])
    return define.DR;

  if (!flag[define.Up] && !flag[define.Down] && flag[define.Left] && flag[define.Right])
    return define.ROW;

  //////////////////////////////////////////////////
  if (flag[define.Up] && !flag[define.Down] && !flag[define.Left] && !flag[define.Right])
    return define.COL;
  if (!flag[define.Up] && flag[define.Down] && !flag[define.Left] && !flag[define.Right])
    return define.COL;
  if (!flag[define.Up] && !flag[define.Down] && flag[define.Left] && !flag[define.Right])
    return define.ROW;
  if (!flag[define.Up] && !flag[define.Down] && !flag[define.Left] && flag[define.Right])
    return define.ROW;

  ////////////////////////////////////////////////
  if (!flag[define.Up] && !flag[define.Down] && !flag[define.Left] && !flag[define.Right])
    return BL;
}

module.exports = {
  initMap: initMap
}